<?php

namespace plugin\property\app;

use plugin\property\app\model\PropertyMenu;
use plugin\property\app\model\PropertyPermission;
use plugin\property\app\model\PropertyRole;
use plugin\property\app\model\PropertyUser;
use plugin\owladmin\app\support\Cores\Menu;
use plugin\property\app\service\PropertySettingService;
use Shopwwi\WebmanAuth\Facade\Auth;
use support\Container;

class Property extends \plugin\owladmin\app\Admin
{

    public static function view($apiPrefix = ''): array|string|null
    {
        $view = file_get_contents(public_path('admin-assets/index.html'));

        $script = '<script>window.$adminApiPrefix = "/property-api"</script>';

        return preg_replace('/<script>window.*?<\/script>/is', $script, $view);
    }

    /**
     * @return Menu;
     */
    public static function menu(): Menu
    {
        return new \plugin\property\app\support\Cores\Menu();
    }


    public static function config($key, $default = '')
    {
        $key = 'plugin.property.' . $key;
        return config($key, $default);
    }

    public static function user(): ?PropertyUser
    {
        return self::guard()->user();
    }

    public static function guard()
    {
        return Auth::guard(self::config('property.auth.guard') ?: 'property');
    }

    public static function setting(): PropertySettingService
    {
        return PropertySettingService::make();
    }

    public static function middleware(): array
    {
        return [
            \plugin\owladmin\app\middleware\ConnectionDatabase::class,
            \plugin\owladmin\app\middleware\ForceHttps::class,
            \plugin\owladmin\app\middleware\AutoSetLocale::class,
            \plugin\owladmin\app\middleware\Authenticate::class,
            \plugin\owladmin\app\middleware\Permission::class,
        ];
    }
}